﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Test
{
    public class PosChant
    {
        #region 医保DLL定义的函数
        public class YbFun
        {
            /// <summary>
            /// 医保交易函数
            /// </summary>
            /// <param name="inputData"></param>
            /// <param name="outputData"></param>
            /// <returns></returns>
            [DllImport("sbyl.dll", CallingConvention = CallingConvention.StdCall)]
            public static extern int CardTrans(StringBuilder inputData, StringBuilder outputData);



            ///// <summary>
            ///// 医保交易函数
            ///// </summary>
            ///// <param name="inputData"></param>
            ///// <param name="outputData"></param>
            ///// <returns></returns>
            //[DllImport("ssse32.dll", CallingConvention = CallingConvention.StdCall)]
            //public static extern int CardTrans(StringBuilder inputData, StringBuilder outputData);


        }


        #endregion


        /*
             字段名称   
                                                
            交易代码        1-2
            交易金额        3-14                 
            pos流水号       15-20          
            收银机号     21-30      
            收银员号    31-40   
            原终端号    41-55
            授权号      56-61
            原交易日期  62-69
            设备类型    70
            第一磁道    71-146
            第二磁道    147-183
            第三磁道      184-287   
         */
        /// <summary>
        /// 格式化输入POS数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private StringBuilder FormateInputString(PosChantEnity entity)
        {
            //入参格式为 业务编号^医疗机构编号^操作员编号^业务周期号^交易流水号^中心编码^交易函数的入参^动态库参数^

            StringBuilder stb = new StringBuilder(2048);
            stb.Append(entity.MerchanCode.Length > 2 ? entity.MerchanCode.Substring(0, 2) : entity.MerchanCode.PadLeft(2, ' '));//交易代码
            stb.Append(entity.MerchanMoney.Length > 12 ? entity.MerchanCode.Substring(0, 12) : entity.MerchanMoney.PadLeft(12, ' '));//交易金额

            stb.Append(entity.POSFlowNo.Length > 6 ? entity.POSFlowNo.Substring(0, 6) : entity.POSFlowNo.PadLeft(6, ' '));//操作员编号

            stb.Append(entity.CashRegNo.Length > 10 ? entity.CashRegNo.Substring(0, 10) : entity.CashRegNo.PadLeft(10, ' '));//收银机号

            stb.Append(entity.CashMemberNo.Length > 10 ? entity.CashMemberNo.Substring(0, 10) : entity.CashMemberNo.PadLeft(10, ' '));//交易流水号

            stb.Append(entity.OrginTerminalNo.Length > 15 ? entity.OrginTerminalNo.Substring(0, 15) : entity.OrginTerminalNo.PadLeft(15, ' '));//原终端号

            stb.Append(entity.AuthorizationNo.Length > 6 ? entity.AuthorizationNo.Substring(0, 6) : entity.AuthorizationNo.PadLeft(6, ' '));//授权号

            stb.Append(entity.OrginTranDT.Length > 8 ? entity.OrginTranDT.Substring(0, 8) : entity.OrginTranDT.PadLeft(8, ' '));
            stb.Append(entity.TerminalType.Length > 1 ? entity.TerminalType.Substring(0, 1) : entity.TerminalType.PadLeft(1, ' '));
            stb.Append(entity.Track1.Length > 76 ? entity.Track1.Substring(0, 76) : entity.Track1.PadLeft(76, ' '));
            stb.Append(entity.Track2.Length > 37 ? entity.Track2.Substring(0, 37) : entity.Track2.PadLeft(37, ' '));
            stb.Append(entity.Track3.Length > 104 ? entity.Track3.Substring(0, 104) : entity.Track3.PadLeft(104, ' '));

            //log.Info("生成输入参数为" + stb.ToString());
            return stb;


        }

        private PosChantOutputEntity FormateOutputString(StringBuilder outputString)
        {
            PosChantOutputEntity entity = new PosChantOutputEntity();
            string str = outputString.ToString();

            //000000                                交易成功000004               52260857100768650710  000000000004                      62103356    98888.99
           

            /*
             
              返回码	        1-6	        6	
            返回码含义	    7-46	    40	
            pos流水号	    43-52	    6	//好像是从43位开始的,注意测试
            授权号	        53-58	    6	
            批次号	        59-64	    6	
            卡号	        65-83	    19	
            有效期	        84-87	    4	
            银行号	        88-89	    2	
            银行检索参考号	90-101	    12	
            终端号	        102-116	    15	
            商户号	        117-131	    15	
            交易金额	    132-143	    12	

             */
            entity.ReturnCode = str.Substring(0, 6).Trim();
            if (str.Length < 128)
            {
                return entity;

            }
            entity.ReturnCodeMean = str.Substring(6, 36).Trim();
            entity.POSFlowNo = str.Substring(42, 6).Trim();
            entity.AuthorizationNo = str.Substring(48, 6).Trim();
            entity.BatchNo = str.Substring(54, 6).Trim();
            entity.CardNo = str.Substring(60, 19).Trim();
            entity.InvalidDate = str.Substring(79, 4).Trim();
            entity.BankNo = str.Substring(83, 2).Trim();
            entity.BankSearchNo = str.Substring(85, 12).Trim();
            entity.TerminalNo = str.Substring(97, 15).Trim();
            entity.FirmNo = str.Substring(112, 15).Trim();
            entity.MerchanMoney = str.Substring(127).Trim();


            return entity;
        }


        public PosChantOutputEntity CandTrans(PosChantEnity entity)
        {
            StringBuilder strOpt = new StringBuilder(1024);



            YbFun.CardTrans(FormateInputString(entity), strOpt);

            return FormateOutputString(strOpt);
        }



        public string CandTransBalance(PosChantEnity entity)
        {
            StringBuilder strOpt = new StringBuilder(1024);



            YbFun.CardTrans(FormateInputString(entity), strOpt);

            return strOpt.ToString();
        }
    }
}
